﻿using NLog;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RMS.Common
{
    public class IISCacheServer
    {
        /// <summary>
        /// 日志记录类
        /// </summary>
        static Logger MyLogger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 时长缓存
        /// </summary>
        private static Hashtable cacheContentExpiry = new Hashtable();
        /// <summary>
        /// 主记录缓存
        /// </summary>
        private static Hashtable cacheContent = new Hashtable();

        /// <summary>
        /// 清除包含指定key值的缓存
        /// </summary>
        /// <param name="key"></param>
        public static void ClearAt(string key)
        {
            if (cacheContentExpiry != null)
            {
                ArrayList cacheContentExpiry_tmp = new ArrayList();

                IDictionaryEnumerator enumerator = cacheContentExpiry.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    if (enumerator.Key.ToString().Contains(key))
                    {
                        cacheContentExpiry_tmp.Add(enumerator.Key.ToString());
                    }
                }
                for (int i = 0; i < cacheContentExpiry_tmp.Count; i++)
                {
                    cacheContentExpiry.Remove(cacheContentExpiry_tmp[i]);
                }
            }
        }
        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public static void ClearAll()
        {
            if (cacheContentExpiry != null)
            {
                cacheContentExpiry.Clear();
            }
        }
        /// <summary>
        /// 设置缓存，有效时间为2小时
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存值</param>
        public static void SetValue(string key, object value)
        {

            cacheContent[key] = value;
            int minus = 120;
            cacheContentExpiry[key] = DateTime.Now.AddMinutes(minus);
            MyLogger.Debug("key:" + key + ",creation at:" + DateTime.Now.ToString() + ", and expiry at :"
                + ((DateTime)cacheContentExpiry[key]).ToString() + ", and the life span is:" + minus.ToString() + ".");
        }
        /// <summary>
        /// 根据key值获取缓存value
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        public static object GetValue(string key)
        {
            if (cacheContentExpiry[key] != null)
            {
                DateTime from = (DateTime)cacheContentExpiry[key];

                if (from.Ticks < DateTime.Now.Ticks)
                {
                    try
                    {
                        cacheContentExpiry.Remove(key);
                        cacheContent.Remove(key);
                    }
                    catch (Exception ex)
                    {
                        MyLogger.Error(ex.Message);
                    }
                    return null;
                }
                else
                {
                    return cacheContent[key];
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("null");
                MyLogger.Debug("cannot find any cache for key:" + key);
                return null;
            }

        }
        /// <summary>
        /// 缓存是否存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        public static bool IsExist(string key)
        {
            return cacheContent.ContainsKey(key);
        }
    }
}
